﻿using System;
using System.Collections.Generic;
using System.Linq;
using ENail.Domain.Core;
using ENail.Domain.Core.Contract.Services;
using Equation.Enterprise.Framework.Web.Model;
using Equation.Enterprise.Utilities.DesignByContract;

namespace ENail.Portal.Web.Models
{
    public class AssignmentViewModel
    {
        //public int RegistrationId { get; set; }
        public int EmployeeId { get; set; }
        public int NailServiceId { get; set; }

        public IList<AssignmentItemViewModel> Assignments { get; set; }
        public IList<AssignmentEmployeeViewModel> Employees { get; set; }
        public IList<AssignmentNailServiceViewModel> NailServices { get; set; }
    }

    public class AssignmentItemViewModel : BaseViewModel<int>
    {
        public string EmployeeName { get; set; }
        public short EmployeeId { get; set; }
        public string ServiceName { get; set; }
        public short NailServiceId { get; set; }
    }

    public class AssignmentEmployeeViewModel : BaseViewModel<Int16>
    {
        public string EmployeeName { get; set; }
    }

    public class AssignmentNailServiceViewModel : BaseViewModel<Int16>
    {
        public string ServiceName { get; set; }
    }
    #region Mapper

    public class AssignmentViewModelMapper
    {
        private readonly IEmployeeService _employeeService;
        private readonly INailServiceService _nailServiceService;

        public AssignmentViewModelMapper(IEmployeeService employeeService, INailServiceService nailServiceService)
        {
            _employeeService = employeeService;
            _nailServiceService = nailServiceService;
        }

        public void MapDomainToView(Registration domainModel, AssignmentViewModel viewModel)
        {
            MapEmployeeDomainListToView(_employeeService.GetAllActiveEmployees(), viewModel);
            MapNailServiceDomainListToView(_nailServiceService.GetAllActiveNailServices(), viewModel);
            MapAssignmnetItemListToView(domainModel.Assignments, viewModel);
        }

        public void MapViewToDomain(AssignmentViewModel viewModel, Registration domainModel)
        {
            //domainModel.Assignments = (from x in viewModel.Assignments
            //                           select new Assignment
            //                                      {
            //                                          Employee = _employeeService.GetEmployee(x.EmployeeId),
            //                                          NailService = _nailServiceService.GetNailService(x.NailServiceId)
            //                                      }).ToList<Assignment>();

            if (viewModel.EmployeeId == 0 || viewModel.NailServiceId == 0)
                return;

            domainModel.AddAssignment(new Assignment
                                          {
                                              Employee = _employeeService.GetEmployee(viewModel.EmployeeId),
                                              NailService = _nailServiceService.GetNailService(viewModel.NailServiceId),
                                          });
        }

        private void MapEmployeeDomainListToView(IList<Employee> domainModels, AssignmentViewModel viewModel)
        {
            viewModel.Employees = (from x in domainModels
                                 where x.IsActive = true
                                 select new AssignmentEmployeeViewModel
                                            {
                                                EmployeeName = x.FirstName + " " + x.LastName,
                                                Id = x.Id
                                            }).ToList<AssignmentEmployeeViewModel>();
        }

        private void MapNailServiceDomainListToView(IList<NailService> domainModels, AssignmentViewModel viewModel)
        {
            viewModel.NailServices = (from x in domainModels
                          where x.IsActive = true
                          select new AssignmentNailServiceViewModel
                                     {
                                         ServiceName = x.ServiceName,
                                         Id = x.Id
                                     }).ToList<AssignmentNailServiceViewModel>();
        }

        private void MapAssignmnetItemListToView(IList<Assignment> assignments, AssignmentViewModel viewModel)
        {
            viewModel.Assignments = (from x in assignments
                          select new AssignmentItemViewModel
                                     {
                                         Id=x.Id,
                                         EmployeeId = x.Employee.Id,
                                         EmployeeName = x.Employee.FirstName + " " + x.Employee.LastName,
                                         NailServiceId = x.NailService.Id,
                                         ServiceName = x.NailService.ServiceName
                                     }).ToList<AssignmentItemViewModel>();
            if (viewModel.Assignments.Count()==0)
            {
                viewModel.Assignments.Add(new AssignmentItemViewModel());
            }
        }
    }

    #endregion

}